// 接收后台消息
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
  const { action } = msg;
  // console.log(`打开页面收到后台的消息: ${action}`);
  action === "on" && delAdDom();
  sendResponse({ res: `打开页面执行的操作是${action}` });
  // chrome.runtime.sendMessage({ host: location.host });
});

// 如果扩展处于开启状态,则删除页面中的广告
window.onload = async function () {
  const res = await chrome.storage.sync.get();
  // console.log(res);
  const isOn = res[location.host] === "on";
  isOn && delAdDom();
};

// 删除页面中的广告
async function delAdDom() {
  document.querySelectorAll("body > div:nth-last-child(-n+4)").forEach((item) => item.parentNode.removeChild(item));
}
